home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / etc / init.d / bootclean < prev    next >
Encoding:
Text File  |  2006-10-06  |  3.5 KB  |  146 lines

  1. #!/bin/sh
  2. #
  3. # bootclean
  4. #
  5. # Clean /tmp
  6. #
  7. # DO NOT RUN AFTER S:55bootmisc.sh and do not run this script directly
  8. # in runlevel S. Instead write an initscript to call it.
  9. #
  10.  
  11. . /lib/init/vars.sh
  12.  
  13. . /lib/lsb/init-functions
  14.  
  15. # Should be called outside verbose message block
  16. mkflagfile()
  17. {
  18.     # Prevent symlink attack  (See #264234.)
  19.     [ -L "$1" ] && log_warning_msg "bootclean: Deleting symbolic link '$1'."
  20.     rm -f "$1" || { log_failure_msg "bootclean: Failure deleting '$1'." ; return 1 ; }
  21.     # No user processes should be running, so no one should be able to introduce
  22.     # a symlink here.  As an extra precaution, set noclobber.
  23.     set -o noclobber
  24.     :> "$1" || { log_failure_msg "bootclean: Failure creating '$1'." ; return 1 ; }
  25.     return 0
  26. }
  27.  
  28. clean_tmp() {
  29.     cd /tmp || { log_failure_msg "bootclean: Could not cd to /tmp." ; return 1 ; }
  30.  
  31.     #
  32.     # Only clean out /tmp if it is world-writable. This ensures
  33.     # it really is a/the temp directory we're cleaning.
  34.     #
  35.     [ "$(find . -maxdepth 0 -perm -002)" = "." ] || return 0
  36.  
  37.     if [ ! "$TMPTIME" ]
  38.     then
  39.         log_warning_msg "Using default TMPTIME 0."
  40.         TMPTIME=0
  41.     fi
  42.  
  43.     [ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /tmp"
  44.  
  45.     #
  46.     # Remove regardless of TMPTIME setting
  47.     #
  48.     rm -f .X*-lock
  49.  
  50.     #
  51.     # Don't clean remaining files if TMPTIME is negative or 'infinite'
  52.     #
  53.     case "$TMPTIME" in
  54.       -*|infinite|infinity)
  55.         [ "$VERBOSE" = no ] || log_action_end_msg 0 "skipped"
  56.         return 0
  57.         ;;
  58.     esac
  59.  
  60.     #
  61.     # Wipe /tmp, excluding system files, but including lost+found
  62.     #
  63.     # If TMPTIME is set to 0, we do not use any ctime expression
  64.     # at all, so we can also delete files with timestamps
  65.     # in the future!
  66.     #
  67.     if [ "$TMPTIME" = 0 ] 
  68.     then
  69.         TEXPR=""
  70.         DEXPR=""
  71.     else
  72.         TEXPR="-mtime +$TMPTIME -ctime +$TMPTIME -atime +$TMPTIME"
  73.         DEXPR="-mtime +$TMPTIME -ctime +$TMPTIME"
  74.     fi
  75.  
  76.     EXCEPT='! -name .
  77.         ! ( -path ./lost+found -uid 0 )
  78.         ! ( -path ./quota.user -uid 0 )
  79.         ! ( -path ./aquota.user -uid 0 )
  80.         ! ( -path ./quota.group -uid 0 )
  81.         ! ( -path ./aquota.group -uid 0 )
  82.         ! ( -path ./.journal -uid 0 )
  83.         ! ( -path ./.clean -uid 0 )
  84.         ! ( -path './...security*' -uid 0 )'
  85.  
  86.     mkflagfile /tmp/.clean || return 1
  87.  
  88.     report_err()
  89.     {
  90.         if [ "$VERBOSE" = no ]
  91.         then
  92.             log_failure_msg "bootclean: Failure cleaning /tmp."
  93.         else
  94.             log_action_end_msg 1 "bootclean: Failure cleaning /tmp"
  95.         fi
  96.     }
  97.  
  98.     #
  99.     # First remove all old files...
  100.     # (Use xargs here so that only one additional process gets created)
  101.     #
  102.     find . -depth -xdev $TEXPR $EXCEPT ! -type d \
  103.         -print0 | xargs -0r rm -f -- \
  104.         || { report_err ; return 1 ; }
  105.  
  106.     #
  107.     # ...and then all empty directories
  108.     # (Don't use xargs here because dirs must be removed one by one from
  109.     # the bottom up)
  110.     #
  111.     find . -depth -xdev $DEXPR $EXCEPT -type d -empty \
  112.         -exec rmdir \{\} \; \
  113.         || { report_err ; return 1 ; }
  114.  
  115.     [ "$VERBOSE" = no ] || log_action_end_msg 0
  116.     return 0
  117. }
  118.  
  119. which find >/dev/null 2>&1 || exit 1
  120. which xargs >/dev/null 2>&1 || exit 1
  121.  
  122. # If there are flag files that have not been created by root
  123. # then remove them
  124. for D in /tmp
  125. do
  126.     if [ -f $D/.clean ]
  127.     then
  128.         which stat >/dev/null 2>&1 && cleanuid="$(stat -c %u $D/.clean)"
  129.         # Poor's man stat %u, since stat (and /usr) might not be
  130.         # available in some bootup stages
  131.         [ "$cleanuid" ] || cleanuid="$(find $D/.clean -printf %U)"
  132.         [ "$cleanuid" ] || { log_failure_msg "bootclean: Could not stat '$D/.clean'." ; exit 1 ; }
  133.         if [ "$cleanuid" -ne 0 ]
  134.         then
  135.             rm -f $D/.clean || { log_failure_msg "bootclean: Could not delete '$D/.clean'." ; exit 1 ; }
  136.         fi
  137.     fi
  138. done
  139.  
  140. [ -f /tmp/.clean ] && exit 0
  141.  
  142. ES=0
  143. [ -d /tmp ] && ! [ -f /tmp/.clean ] && { clean_tmp || ES=1 ; }
  144. exit $ES
  145.  
  146.